package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    //待审核travel
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelQueryObject qo, Model model) {
        qo.setState(Travel.STATE_WART);
        qo.setIsPublic(true);
        qo.setOrderBy("t.createTime desc");
        model.addAttribute("result", travelService.selectTravels(qo));
        return "travel/list";
    }

    //已发布travel
    @RequestMapping("/releaseList")
    public String releaseList(@ModelAttribute("qo") TravelQueryObject qo, Model model) {
        qo.setState(Travel.STATE_RELEASE);
        qo.setIsPublic(true);
        qo.setOrderBy("t.releaseTime desc");
        model.addAttribute("result", travelService.selectTravels(qo));
        return "travel/releaseList";
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(Travel t) {
        JSONResult json = new JSONResult();
        try {
            travelService.updateStateById(t);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @RequestMapping("/selectOne")
    @ResponseBody
    public Object selectOne(Long id) {
        TravelContent content = travelService.selectTravelContentById(id);
        return content;
    }


}
